package com.luoran.car.controller;


import cn.hutool.jwt.JWT;
import cn.hutool.jwt.signers.JWTSignerUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.luoran.car.domain.Syscustomer;
import com.luoran.car.domain.Useer;
import com.luoran.car.domain.UserKuhu;
import com.luoran.car.service.SyscustomerService;
import com.luoran.car.service.UseerService;
import com.luoran.car.service.UserKuhuService;
import com.luoran.car.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("car")
public class UserController {

    @Autowired
    SyscustomerService syscustomerService;

    @RequestMapping("/login")
    public Result list(@RequestBody Syscustomer syscustomer)
    {
        LambdaQueryWrapper<Syscustomer> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Syscustomer::getCustomerPhone,syscustomer.getCustomerPhone());
        Syscustomer one = syscustomerService.getOne(lambdaQueryWrapper);
        if(one==null)
        {
            return Result.fail("用户名错误");
        }
        if(!one.getPassword().equals(syscustomer.getPassword()))
        {
            return Result.fail("密码错误");
        }

        String token = JWT.create()
                .setPayload("id", one.getCustomerId())
                .setPayload("username", one.getCustomerName())
                .setPayload("admin", true)
                .setSigner(JWTSignerUtil.none())
                .sign();

        HashMap<String, String> map = new HashMap<>();

        map.put("token",token);
        map.put("id",one.getCustomerId()+"");
        map.put("name",one.getCustomerName()+"");

        return Result.success(map);

    }







}




